﻿Imports System.Windows.Forms

Public Class frmOrdersOnShipment
    Inherits dbiWSF.Dialogs.dlgDefault

    'Local variables to track the shipment and loading dock on which the shipment is scheduled.
    Dim l_shipment As dbiWSF.FrameworkObjects.objShipment
    Dim l_loadingdock As dbiWSF.FrameworkObjects.objLoadingDock

    Public Sub New(ByVal Shipment As dbiWSF.FrameworkObjects.objShipment, ByVal LoadingDock As dbiWSF.FrameworkObjects.objLoadingDock)
        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        'Sets the form's variables to the values passed in when the form is created.
        l_loadingdock = LoadingDock
        l_shipment = Shipment

        'Create the columns
        Dim Column1 As New Dbi.WinControl.dbiColumnItem("Order", 200)
        Dim Column2 As New Dbi.WinControl.dbiColumnItem("CargoType(s)", 300)
        Dim Column3 As New Dbi.WinControl.dbiColumnItem("On Shipment")

        Column3.DataType = Dbi.WinControl.enumDataType.Boolean
        Column3.Format.CheckBox = Dbi.enumCheckBox.Standard

        'Add the columns to the dbiList's Columns collection.
        Me.DbiList1.Columns.Add(Column1)
        Me.DbiList1.Columns.Add(Column2)
        Me.DbiList1.Columns.Add(Column3)

        'Set the title for the form.
        If l_shipment.ShipmentType = dbiWSF.FrameworkObjects.ShipmentTypes.Outbound Then
            Me.DbiLabel1.Text = "Purchase Orders on this Shipment ..."
        Else
            Me.DbiLabel1.Text = "Invoices on this Shipment ..."
        End If

    End Sub

    Private Sub OK_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.OK

        'Iterate through the items in the list
        For Each curNode As Dbi.WinControl.dbiNodeItem In Me.DbiList1.Items

            If curNode.GetCellText(2) = "0" Then
                'If the user unchecked the checkbox in the last column remove the order from the shipment.
                'NOTE:  Remove the ShipmentID from the OrderHeader thereby removing the link between the two at the data layer.
                curNode.Tag.ShipmentID = System.Guid.Empty

                'Remove the OrderHeader from the collection stored on the shipment object.
                l_shipment.OrderHeaders.Remove(curNode.Tag)

            End If

        Next    'Node item.

        Me.Close()

    End Sub

    Private Sub Cancel_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel_Button.Click
        Me.DialogResult = System.Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub DbiList1_FirstDraw(ByVal sender As Object, ByVal e As System.EventArgs) Handles DbiList1.FirstDraw
        'NOTE: This event fires when the dbiList control is first drawn in the form.

        Dim OrderType As String

        'Determine which type of orders are applicable to the shipment based on the shipment type.
        If l_shipment.ShipmentType = dbiWSF.FrameworkObjects.ShipmentTypes.Outbound Then
            OrderType = "Invoice"
        Else
            OrderType = "Purchase Order"
        End If

        Me.DbiList1.TitleText = "Add Orders to Shipment: " + l_shipment.Description

        'Create a collection of order headers.
        Dim myOrders As New System.Collections.Generic.List(Of dbiWSF.FrameworkObjects.objOrderHeader)
        myOrders = l_shipment.OrderHeaders

        'Loop through all of the OrderHeaders on the shipment and populate the list with their details.
        For Each Order As dbiWSF.FrameworkObjects.objOrderHeader In myOrders

            If Order.OrderEntryType = OrderType Then
                'Use a LINQ query to select a distinct count of cargo types on each order.
                'NOTE:  This is used to avoid displaying redundant information to the user.
                Dim OrderCargo = From OrderDetails As dbiWSF.FrameworkObjects.objOrderDetail In Order.OrderDetails _
                                 Order By OrderDetails.CargoTypeID _
                                 Distinct Select OrderDetails.CargoTypeID

                If OrderCargo.Count > 0 Then

                    'Create a new node item for the list.
                    Dim newNode As New Dbi.WinControl.dbiNodeItem(Order.OrderEntryID + ";")

                    'Build the cargo type description.
                    For Each Cargo As System.Guid In OrderCargo.Distinct
                        newNode.Text += " | " + myWSFObjects.GetCargoDescripFromID(Cargo)
                    Next

                    'Check in the last column to indicate it has been added to the shipment.
                    'NOTE: If the user un checks the column the order will be removed from the shipment
                    newNode.Text += ";1"

                    'Place the order object in the tag property of the node item representing it.
                    newNode.Tag = Order

                    'Iterate through the order details related to the order header.
                    For Each OrderDetail As dbiWSF.FrameworkObjects.objOrderDetail In Order.OrderDetails

                        'Create a new node item to represent the order detail.
                        Dim DetailNode As New Dbi.WinControl.dbiNodeItem()
                        Dim x As New NorthwindProduct

                        'Determine from which table to select the information on the items in the order.
                        'NOTE:  This is required because the Sales (Invoice) and Purchase Order details are stored in separate tables in the northwinds database
                        If l_shipment.ShipmentType = dbiWSF.FrameworkObjects.ShipmentTypes.Inbound Then
                            x = GetProductByID(dtPODetails.Rows.Find(OrderDetail.LineNumber).Item("Product ID").ToString)
                        Else
                            x = GetProductByID(dtOrderDetails.Rows.Find(OrderDetail.LineNumber).Item("Product ID").ToString)
                        End If

                        'Set the column values for the node item.
                        DetailNode.Text = OrderDetail.LineNumber.ToString + ":  " + x.ProductName + " | " + myWSFObjects.GetCargoDescripFromID(OrderDetail.CargoTypeID)

                        'Store the detail object in the tag property of the node item representing it.
                        DetailNode.Tag = OrderDetail

                        'Make the new detail node a header item.
                        DetailNode.Header = True

                        'Add the detail node item to the order header node item.
                        newNode.Items.Add(DetailNode)

                    Next    'Order Detail

                    'Add the order header node to the dbiList control.
                    Me.DbiList1.Items.Add(newNode)

                End If

            End If

        Next    'Order Header

        'Apply the current style, selected in the main application, to the dbiList control
        Dim curStyle As New dbiWSF.ControlLibrary.dbiWSFStyleControls
        Dim curListOptions As New dbiWSF.ControlLibrary.dbiStylerOptions.dbiListStyleOptions

        curStyle.ImportXMLWithXMLString(curListOptions, Me.DbiList1, dbiWSF.ControlLibrary.dbiStylerOptions.GetResources(curStyleName).dbiListXML)

    End Sub

End Class
